﻿


using HBase;
using hconnect;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Controller {
    public class CarrierSynchro1 : TestableTask {
        protected CarrierConnect1 connect = null;
        public IMServer mServer = GuidangFactray.getDal<IMServer>();
        public CarrierSignalStatus dd;
 
      
    

        public override bool excute() {
            time = 1000;
            /*
            for (ushort i = 0; i < 3500 * 4; i++) {
                byte[] data1 = connect.getData(i, 1);
                if (data1 == null) {
                    fileAll(1, i + "读不到");
                } else {
                    for (int j = 0; j < data1.Length; j++) {
                        if (data1[j] == 'a') {
                            fileAll(1, i + "读到了a");
                        }
                        if (data1[j] == 'b') {
                            fileAll(1, i + "读到了b");
                        }
                        if (data1[j] == 'c') {
                            fileAll(1, i + "读到了c");
                        }
                        if (data1[j] == 'd') {
                            fileAll(1, i + "读到了d");
                        }
                    }
                }
            }*/
            byte[] data = connect.getData(7000, 45);

            if (data != null) {

                for (int i = 0; i < 41; i++) {
                    byte b = data[i * 2];
                    data[i * 2] = data[i * 2 + 1];
                    data[i * 2 + 1] = b;

                }

                dd = new CarrierSignalStatus();

             
                dd.type = S7ByteUtil.toString(data, 0, 10);
                dd.gg = S7ByteUtil.toString(data, 12, 20);
                dd.lotNo = S7ByteUtil.toString(data, 32, 10);
                dd.mz = toFloat(data, 44) + "KG";
                dd.jz = toFloat(data, 48) + "KG";

                dd.roll = S7ByteUtil.simotionInt32(data, 52) + "卷";
                dd.code = S7ByteUtil.toString(data, 56, 18);
                int s = S7ByteUtil.simotionInt32(data, 76);
                dd.canWork = s;
                dd.sysId = null;
                String info = Newtonsoft.Json.JsonConvert.SerializeObject(dd);
                info=info.Replace("\"", "").Replace(" ","").Replace("{","").Replace("}", "");
                if (s == 1) {
                    fileAll(1, "打印" + info);
                    QRHFPrinter mpd = new QRHFPrinter();
                    mpd.print(dd);
                    connect.write(7040, 1, "打印完成");
                    fileAll(1, "打印结束"+ info);
                    oneTimeSleep = 5000;
                } else {
                    consoleOnly(1,"未打印"+ info);
                }
              

            } else {
                fileAll(1, "读失败");
            }

            return true;



        }

        public static float toFloat(byte[] b, int begin) {
            if (b == null) {
                return 0f;
            }

            byte[] value = new byte[4]
            {
                b[begin + 0],
                b[begin + 1],
                b[begin + 2],
                b[begin+3]
            };
            return BitConverter.ToSingle(value, 0);
        }
    }
}
